package com.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.entity.GoodsDetail;
import com.goods.mapper.GoodsDetailMapper;
import com.goods.service.GoodsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cd
 * @version 1.0
 * @date 2021/10/6 20:45
 */
@Service
public class GoodsDetailServiceImpl implements GoodsDetailService {
    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insert(GoodsDetail goodsDetail) {
        int i = goodsDetailMapper.insert(goodsDetail);
        if (i != 1) {
            System.out.println("添加商品失败");
            throw new RuntimeException("添加商品失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateById(GoodsDetail goodsDetail) {
        int i = goodsDetailMapper.updateByPrimaryKeySelective(goodsDetail);
        if (i != 1) {
            System.out.println("修改商品失败");
            throw new RuntimeException("修改商品失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteById(Integer id) {
        int i = goodsDetailMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            System.out.println("删除商品失败");
            throw new RuntimeException("删除商品失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)

    public GoodsDetail findById(Integer id) {
        return goodsDetailMapper.findById(id);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public PageInfo<GoodsDetail> findBySortIdAndPage(Integer page, Integer rows, Integer sortId) {
        //1. 分页
        PageHelper.startPage(page, rows);

        //2. 查询
        List<GoodsDetail> DetailList = goodsDetailMapper.findBySortId(sortId);

        //3. 封装PageInfo
        PageInfo<GoodsDetail> pageInfo = new PageInfo<>(DetailList);

        //4. 返回
        return pageInfo;
    }
}
